package com.yzz.hzraisedust.hzraseidust.main;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.Layout;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.yzz.hzraisedust.hzraseidust.R;

import com.yzz.hzraisedust.hzraseidust.modelbean.MapItemBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhengzhen on 2017/8/20.
 */

public class DropDownList implements AdapterView.OnItemClickListener,PopupWindow.OnDismissListener ,View.OnClickListener{
    private PopupWindow mPop;
    private Context mContext;
    private View mShowPopWindow;
    private ListView mListView;
    private MyAapter mAdapter;
    private List<MapItemBean> items;
    private List<MapItemBean> showItems;
    private View mParentLinearLayout;

    private ImageView drownImage;
    private EditText mEditText;
    private TextView mTextView;

    private CallBack mCallBack;
    public interface  CallBack{
        void searchItem(MapItemBean itemBean);
    }
    public DropDownList(Context context,View parent,CallBack callBack) {
        mContext = context;
        mParentLinearLayout = parent;
        drownImage = parent.findViewById(R.id.search_project_item_image);

        mTextView = parent.findViewById(R.id.search_project_item);
        showItems = new ArrayList<>();
        this.mCallBack = callBack;
    }

    public void setItems(List<MapItemBean> items){
        this.items = items;
        for(MapItemBean item :items){
            showItems.add(item);
        }
        init();
        initPop();
    }

    private void initLensener() {
        mEditText.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                String str = s.toString();
                showItems.clear();

                if(str.equals("")){
                    for(MapItemBean item : items){
                            showItems.add(item);
                    }
                }else {
                    for (MapItemBean item : items) {
                        if (item.getName().contains(str))
                            showItems.add(item);
                    }
                }
                mAdapter.notifyDataSetChanged();


            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        });
        drownImage.setOnClickListener(this);
    }

    public void init() {
        mShowPopWindow=  LayoutInflater.from(mContext).inflate(
                R.layout.drop_down_list_listview_layout, null);
        mListView =  mShowPopWindow.findViewById(R.id.list_drop_down_list);
        mEditText = mShowPopWindow.findViewById(R.id.search_project_item_edite);
        ((LinearLayout)mShowPopWindow).removeView(mShowPopWindow); //不显示用户提示列表
        mListView.setOnItemClickListener(this); // 点选用户
        mAdapter = new MyAapter(showItems);
        mListView.setAdapter(mAdapter);
        initLensener();
    }

    public void showPop() {

        if(!mPop.isShowing()&&showItems.size()>0)
        {
            drownImage.setImageResource(R.mipmap.login_more_down);
            mPop.showAsDropDown(mParentLinearLayout, 2, 1);

        }

    }
    public void initPop() {
       int width = mParentLinearLayout.getWidth() - 4;
       int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mPop = new PopupWindow(mShowPopWindow, width, height, true);

        mPop.setOnDismissListener(this);// pop消失的监听事件
        // 设置弹出框背景
        mPop.setBackgroundDrawable(new ColorDrawable(0xffffffff));

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        MapItemBean item = showItems.get(i);
        if(mCallBack!=null){
            mCallBack.searchItem(item);
        }
        mTextView.setText(item.getName());
        mEditText.setText("");
        mPop.dismiss();
    }

    @Override
    public void onDismiss() {
        drownImage.setImageResource(R.mipmap.login_more_up);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.search_project_item_image){
            showPop();
        }
    }

    /* ListView的数据源适配器*/
    class MyAapter extends ArrayAdapter<MapItemBean> {

        public MyAapter(List<MapItemBean> users) {
            super(mContext, 0, users);
        }

        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.listview_item, null);
            }

            TextView userIdText = (TextView) convertView
                    .findViewById(R.id.listview_userid);
            userIdText.setText(getItem(position).getName());

            ImageView deleteUser = (ImageView) convertView
                    .findViewById(R.id.login_delete_user);
            deleteUser.setVisibility(View.GONE);
            return convertView;
        }

    }

}
